﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace YJKDataBase
{
	public partial class DlgCompare : System.Windows.Forms.Form
	{
		public List<CategoryForAdd> CategoryList
		{
			get
			{
				List<CategoryForAdd> list = new List<CategoryForAdd>();
				this.getChildNodes(this.tvCategory.Nodes[0], list);
				return list;
			}
		}

		private void getChildNodes(TreeNode node, List<CategoryForAdd> categoryList)
		{
			foreach (object obj in node.Nodes)
			{
				TreeNode treeNode = (TreeNode)obj;
				if (treeNode.Tag is CategoryForAdd)
				{
					categoryList.Add((CategoryForAdd)treeNode.Tag);
				}
				foreach (object obj2 in treeNode.Nodes)
				{
					TreeNode treeNode2 = (TreeNode)obj2;
					if (treeNode2.Tag is CategoryForAdd)
					{
						categoryList.Add((CategoryForAdd)treeNode2.Tag);
					}
					this.getChildNodes(treeNode2, categoryList);
				}
			}
		}

		public DlgCompare(RelationDatabase tagDb, RelationDatabase newDb)
		{
			this.mTagDb = tagDb;
			this.mNewDb = newDb;
			this.InitializeComponent();
		}

		private void DlgCompare_Load(object sender, EventArgs e)
		{
			this.mTagDb.Compare(this.mNewDb, this.tvCategory);
		}

		private void tvCategory_AfterSelect(object sender, TreeViewEventArgs e)
		{
			if (this.tvCategory.SelectedNode.Tag is CategoryForAdd)
			{
				this.lbxFamily.Items.Clear();
				foreach (ObjectForAdd objectForAdd in (this.tvCategory.SelectedNode.Tag as CategoryForAdd).FamilyList)
				{
					this.lbxFamily.Items.Add(objectForAdd, objectForAdd.Add);
				}
			}
		}

		private void btnOK_Click(object sender, EventArgs e)
		{
		}

		private void toolStripMenuItem1_Click(object sender, EventArgs e)
		{
			this.cmdSearch();
		}

		private void cmdSearch()
		{
			this.lbxFamily.Items.Clear();
			List<DbObject> list = this.mNewDb.ObjectTable.SearchFamily(this.tstbSearchName.Text);
			if (list.Count > 0)
			{
				foreach (DbObject item in list)
				{
					this.lbxFamily.Items.Add(item, true);
				}
			}
			this.lbxFamily.Focus();
		}

		private void 全选ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetItemCheckState(i, CheckState.Checked);
			}
		}

		private void 反选ToolStripMenuItem_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				CheckState checkState = this.lbxFamily.GetItemCheckState(i);
				if (checkState == CheckState.Checked)
				{
					checkState = CheckState.Unchecked;
				}
				else if (checkState == CheckState.Unchecked)
				{
					checkState = CheckState.Checked;
				}
				this.lbxFamily.SetItemCheckState(i, checkState);
			}
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				this.lbxFamily.SetItemCheckState(i, CheckState.Checked);
			}
		}

		private void btnSelChange_Click(object sender, EventArgs e)
		{
			for (int i = 0; i < this.lbxFamily.Items.Count; i++)
			{
				CheckState checkState = this.lbxFamily.GetItemCheckState(i);
				if (checkState == CheckState.Checked)
				{
					checkState = CheckState.Unchecked;
				}
				else if (checkState == CheckState.Unchecked)
				{
					checkState = CheckState.Checked;
				}
				this.lbxFamily.SetItemCheckState(i, checkState);
			}
		}

		private void lbxFamily_SelectedIndexChanged(object sender, EventArgs e)
		{
		}

		private void lbxFamily_ItemCheck(object sender, ItemCheckEventArgs e)
		{
			if (this.tvCategory.SelectedNode.Tag is CategoryForAdd && this.lbxFamily.SelectedItem != null)
			{
				CategoryForAdd categoryForAdd = (CategoryForAdd)this.tvCategory.SelectedNode.Tag;
				ObjectForAdd objectForAdd = this.lbxFamily.Items[e.Index] as ObjectForAdd;
				if (objectForAdd != null)
				{
					if (e.NewValue == CheckState.Checked)
					{
						using (List<ObjectForAdd>.Enumerator enumerator = categoryForAdd.FamilyList.GetEnumerator())
						{
							while (enumerator.MoveNext())
							{
								ObjectForAdd objectForAdd2 = enumerator.Current;
								if (objectForAdd2.ToString() == objectForAdd.ToString())
								{
									objectForAdd2.Add = true;
									break;
								}
							}
							return;
						}
					}
					foreach (ObjectForAdd objectForAdd3 in categoryForAdd.FamilyList)
					{
						if (objectForAdd3.ToString() == objectForAdd.ToString())
						{
							objectForAdd3.Add = false;
							break;
						}
					}
				}
			}
		}

		private void lbxFamily_Click(object sender, EventArgs e)
		{
		}

		private void lbxFamily_MouseClick(object sender, MouseEventArgs e)
		{
			if (this.lbxFamily.SelectedItem != null && ((ListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.lbxFamily.ClearSelected();
			}
		}

		private void lbxFamily_MouseDoubleClick(object sender, MouseEventArgs e)
		{
			if (this.lbxFamily.SelectedItem != null && ((ListBox)sender).IndexFromPoint(e.X, e.Y) == -1)
			{
				this.lbxFamily.ClearSelected();
			}
		}

		private RelationDatabase mTagDb;

		private RelationDatabase mNewDb;
	}
}
